using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysFrame.Message;
using System.Reflection;

namespace SysFrame.BP
{
    /// <summary>
    /// 人员表
    /// </summary>
    public class BLLsystem_1
    {
        public Isystem_1 interface_system_1;

        public BLLsystem_1()
        {
            object obj = BPBase.GetAchieve("SysFrame.BP", "system_1");
            interface_system_1 = (Isystem_1)obj;
        }


        /// <summary>
        /// 得到system_1实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Mssystem_1> Getsystem_1MsByQuery(MsQuery msq)
        {
            return interface_system_1.Getsystem_1MsByQuery(msq);
        }

        /// <summary>
        /// 通过System_1_10得到实体
        /// </summary>
        /// <param name="System_1_10">ID</param>
        /// <returns>实体</returns>
        public Mssystem_1 Getsystem_1MsByKey(string System_1_10)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and System_1_10='" + System_1_10 + "' ";
            List<Mssystem_1> lstData = Getsystem_1MsByQuery(msquery);
            return lstData[0];
        }

        /// <summary>
        ///  保存system_1信息
        /// </summary>
        /// <param name="ms">实体信息</param>
        /// <returns>消息</returns>
        public MsReturned Savesystem_1(Mssystem_1 ms)
        {
            return interface_system_1.Savesystem_1(ms);
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Getsystem_1ByQuery(MsQuery msquery)
        {
            return interface_system_1.Getsystem_1ByQuery(msquery);
        }

        /// <summary>
        /// 删除system_1
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_1ByQuery(MsQuery msq)
        {
            return interface_system_1.Deletesystem_1ByQuery(msq);
        }
        /// <summary>
        /// 删除system_1
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deletesystem_1ByKey(string System_1_10)
        {
            MsQuery msquery = new MsQuery();

            if (System_1_10.IndexOf(",") > 0)
            {
                msquery.strquery += " and System_1_10 in (" + System_1_10 + ") ";
            }
            else
            {
                msquery.strquery += " and System_1_10='" + System_1_10 + "' ";
            }
            return interface_system_1.Deletesystem_1ByQuery(msquery);
        }

        /// <summary>
        /// 用户登录验证方法
        /// </summary>
        /// <param name="username"></param>
        /// <param name="psw"></param>
        /// <returns></returns>
        internal MsReturned UserLode(string username, string psw)
        {
            //判断用户是否存在
            //加密算法EncryptDES.strEncryptToDES
            //删除标志 SYSTEM_1_90 !='1'
            MsReturned msr = new MsReturned();
            MsQuery msq = new MsQuery();
            msq.strquery += " and SYSTEM_1_20 ='" + username + "'";
            msq.strquery += " and (SYSTEM_1_90 !='1' or SYSTEM_1_90 is null)";
            List<Mssystem_1> listms = Getsystem_1MsByQuery(msq);
            if (listms.Count != 1)
            {
                msr.MsRbool = false;
                msr.strMsId = "-1";//用户名不存在
                msr.strMS = "用户名" + username + "不存在";
            }
            else
            {
                psw = EncryptDES.strEncryptToDES(psw);
                if (listms[0].System_1_30 == psw)
                {
                    msr.MsRbool = true;
                    msr.strMsId = "1";//用户名存在
                    msr.strMS = "验证成功";
                    msr.obj1 = listms[0];
                    //得到角色信息
                    listms[0].sys12 = new Mssystem_12();
                    listms[0].sys10 = new Mssystem_10();
                    /*超级用户admin拥有最终权限*/
                    if (!listms[0].isadmin)
                    {
                        listms[0].sys10 = GetSys10ByUserID(listms[0].System_1_10);
                    }
                    else
                    {
                        listms[0].sys10.System_10_10 = -999;
                    }
                    //得到菜单ID集合
                    listms[0].menuids = GetMenuIdsBySys10ID(listms[0].sys10.System_10_10);
                    BLLsystem_16.LoginLog(listms[0].System_1_10.ToString(), true, "");
                }
                else
                {
                    msr.MsRbool = false;
                    msr.strMsId = "-2";//用户名不存在
                    msr.strMS = "密码错误";
                    BLLsystem_16.LoginLog(listms[0].System_1_10.ToString(), false, "密码错误");
                }
            }

            return msr;

        }


        /// <summary>
        /// 得到角色ID
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public Mssystem_10 GetSys10ByUserID(int userid)
        {
            Mssystem_10 ms10 = new Mssystem_10();
            try
            {
                MsQuery msq = new MsQuery();
                //得到角色ID
                Mssystem_r3 msr3 = new Mssystem_r3();
                msr3 = new BLLsystem_r3().Getsystem_r3MsByQuery(new MsQuery(" and SYSTEM_R3_20='" + userid + "' and SYSTEM_R3_40 =1 "))[0];
                ms10 = new BLLsystem_10().Getsystem_10MsByKey(msr3.System_R3_30);
            }
            catch { }
            return ms10;
        }

        /// <summary>
        /// 得到菜单ID
        /// </summary>
        /// <param name="sys10id"></param>
        /// <returns></returns>
        private List<int> GetMenuIdsBySys10ID(int sys10id)
        {
            List<int> ids = new List<int>();
            ids.Add(-1);
            if (sys10id == null)
            {
                return ids;
            }
            try
            {
                MsQuery msq = new MsQuery();
                //超级管理员
                if (sys10id != -999)
                {
                    msq.strquery += " and SYSTEM_12_40 = 1 ";
                    msq.strquery += " and SYSTEM_12_20 = '" + sys10id + "'";
                    List<Mssystem_12> listsys12 = new List<Mssystem_12>();
                    listsys12 = new BLLsystem_12().Getsystem_12MsByQuery(msq);
                    List<int> meunid = new List<int>();
                    meunid = (List<int>)listsys12.ConvertAll(m => m.System_12_30);
                    ids = (List<int>)ids.Concat(meunid).ToList();

                }
                else
                {
                    msq.strquery += " and SYSTEM_7_40 = 1 ";
                    List<Mssystem_7> ms7 = new List<Mssystem_7>();
                    ms7 = new BLLsystem_7().Getsystem_7MsByQuery(msq);
                    List<int> meunid = new List<int>();
                    meunid = (List<int>)ms7.ConvertAll(m => m.System_7_10);
                    ids = (List<int>)ids.Concat(meunid).ToList();
                }
            }
            catch { }
            return ids;
        }
        /// <summary>
        /// 得到所有按钮通过菜单ID
        /// </summary>
        /// <param name="MenuId">菜单ID</param>
        /// <returns></returns>
        public static List<string> GetAllBtnByMenu(string MenuId)
        {
            List<string> allbtn = new List<string>();
            List<Mssystem_8> listms8 = new List<Mssystem_8>();
            MsQuery msq = new MsQuery();
            msq = new MsQuery();
            msq.strquery += " and SYSTEM_8_40 in (select SYSTEM_7_30 from SYSTEM_7 where SYSTEM_7_10='" + MenuId + "')";
            listms8 = new BLLsystem_8().Getsystem_8MsByQuery(msq);
            if (listms8 != null)
            {
                allbtn = listms8.ConvertAll(m => m.System_8_30);
            }
            return allbtn;
        }
        /// <summary>
        /// 通过角色ID菜单ID 得到 按钮ID 不显示的 隐藏
        /// </summary>
        /// <param name="sys10id">角色ID</param>
        /// <param name="MenuId">菜单ID</param>
        /// <param name="b">是否显示</param>
        /// <returns></returns>
        public static List<string> GetUserBtnByMenu(int sys10id, string MenuId, bool b)
        {
            try
            {
                //得到角色 菜单 按钮
                List<Mssystem_13> listms = new List<Mssystem_13>();
                MsQuery msq = new MsQuery();
                msq.strquery += " and SYSTEM_13_20 = '" + sys10id + "' ";
                msq.strquery += " and SYSTEM_13_30 = '" + MenuId + "' ";
                msq.strquery += " and (SYSTEM_13_70 = '1' or SYSTEM_13_70 is null) ";
                listms = new BLLsystem_13().Getsystem_13MsByQuery(msq);
                List<string> showbtn = new List<string>();
                List<string> allbtn = GetAllBtnByMenu(MenuId);
                if (listms != null)
                {
                    try
                    {
                        //按钮ID获取按钮名称
                        showbtn = listms.ConvertAll(m => m.System_13_50);
                        showbtn.Add("-1");
                        msq = new MsQuery();
                        msq.strquery += " and SYSTEM_8_10 in (" + string.Join(",", showbtn) + ")";
                        showbtn = (new BLLsystem_8().Getsystem_8MsByQuery(msq)).ConvertAll(m => m.System_8_30);
                    }
                    catch { }
                }
                //得到所有BTN 先找到功能ID 然后找功能的按钮

                List<string> btns = new List<string>();
                if (!b)
                {
                    btns = allbtn.Except(showbtn).ToList();
                }
                else
                {
                    btns = showbtn;
                }

                return btns;
            }
            catch { return null; }
        }
    }
}
